package
{
	import flash.display.BitmapData;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTriangleFace;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.textures.Texture;

	/**
	 * 
	 * @author Polar
	 * @date 2013-3-5
	 * @function 
	 * 
	 */
	[SWF(width="640",height="480",frameRate="60",backgroundColor="#FFFFFF")]
	public class Tutorial_01 extends Main3D
	{
		[Embed(source="res/texture.jpg")]
		private static const JPGTex:Class;
		
		private var tex:Texture;
		public function Tutorial_01()
		{
			super();
		}
		
		protected override function main():void
		{
			backgroundColor = 0xcccccc;
			
			tex = generateMipMap(new JPGTex,512);
			
			var agal_vertex:String = "";
			agal_vertex += "mov op,va0 \n";	
			agal_vertex += "mov v0,va1 \n";	//color
			agal_vertex += "mov v1,va2 \n";	//texcoord
			
			var agal_fragment:String = "";
			agal_fragment += "tex ft1,v1,fs0<2d,liner,nomip> \n";
			agal_fragment += "mul ft1,v0,ft1 \n";
			agal_fragment += "mov oc,ft1";
			
			
			createProgram(agal_vertex,agal_fragment);
			
			var vertexData:Vector.<Number> = new <Number>
				[
					-0.9,	0.9,	0,	0,0,1,	0,0,
					0.9,	0.9,	0,	1,0,0,	1,0,
					-0.9,	-0.9,	0,	0,1,0,	0,1,
					0.9,	-0.9,	0,	1,0,0,	1,1,
				];
			
			var indexData:Vector.<uint> = new <uint>
				[
					0,1,2,2,1,3,
				];
			setVertexData(8,vertexData);
			setIndexData(indexData);
			
			context3D.setVertexBufferAt(0,_vertexBuffer,0,Context3DVertexBufferFormat.FLOAT_3);
			context3D.setVertexBufferAt(1,_vertexBuffer,3,Context3DVertexBufferFormat.FLOAT_3);
			context3D.setVertexBufferAt(2,_vertexBuffer,6,Context3DVertexBufferFormat.FLOAT_2);
			context3D.setCulling(Context3DTriangleFace.NONE);
		}
		
		protected override function draw():void
		{
			super.draw();
			
			program = _shaderProgram;
//			context3D.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,0,new Vector.<Number>([0,1,1,0]));
			context3D.setTextureAt(0,tex);
			context3D.drawTriangles(_indexBuffer,0,2);
		}

	}
}